package org.lq.us.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.lq.us.util.ConfigHelper;

import com.google.gson.Gson;

/**
 * Servlet implementation class IconFileUpload
 */
@WebServlet("/IconFileUpload")
@MultipartConfig
public class IconFileUpload extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public IconFileUpload() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Part part = request.getPart("file");
		if(part!=null) {
			String fileName= part.getSubmittedFileName();
			String fileExt= fileName.substring(fileName.lastIndexOf("."));
			String name= UUID.randomUUID().toString().replaceAll("-", "");
			String iconName = name+fileExt;
			part.write(ConfigHelper.getUploadPath()+"\\"+iconName);
			//{"code":0,"msg":"fdfs"}
			Map<String,Object> map = new HashMap<>();
			map.put("code",0);
			map.put("fileName",iconName);
			response.getWriter().print(new Gson().toJson(map));
			
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
